<?php

class Ubahpass extends Controller {

    function Ubahpass()
    {
        parent::Controller();
        $this->load->helper('directory');

        if(!$this->session->userdata('id_admin'))
            redirect('admin/login');
    }

    function index()
    {
        // jangan ke sini kalo belom login
        if(!$this->session->userdata('id_admin'))
            redirect('admin/login');

        // kalo form disubmit
        if ($this->input->post('submit'))
        {
            // validasi dulu

            $this->load->library('form_validation');

            $this->form_validation->set_rules('input_password2', 'Password', 'required|matches[input_password3]');
            $this->form_validation->set_rules('input_password3', 'Konfirmasi password', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                // on error
            }
            else
            {
                // on success: update

                $data_profil = array(
                    'password' => md5($this->input->post('input_password2')),
                );

                $this->db->where('id_admin', $this->session->userdata('id_admin'));
                $this->db->update('tb_admin', $data_profil);

                $data['updated'] = true;
            }
        }

        $data['judul'] = "Ubah Password";
        $data['template_konten'] = 'admin/admin_ubahpass';

        $this->load->vars($data);
        $this->load->view('admin/template');
    }

}